<template>
  <div class="todoapp" id="app">
    <Header :list="list" @add-todo="addTodo" />
    <List @del-todo="delTodo" :list="newList" />
    <Footer
      :list="list"
      :state="state"
      @change-state="changeState"
      @del-done="delDone"
    />
  </div>
</template>

<script>
import Header from "./views/header.vue";
import List from "./views/list.vue";
import Footer from "./views/footer.vue";
import "./styles/base.css";
import "./styles/index.css";
/**
 * 数据切换
 * 需求：点击对应按钮 显示不同列表数据
 * 1.按钮点击高亮
 * 思路：
 * 1.定义一个标识falg(index, string, id等)，代表当前需要高亮的元素
 * 2.根据flag对比其他有flag的元素，获取点击的元素
 *
 * 2.对应高亮的按钮->切换数据
 */

export default {
  name: "App",
  components: {
    Header,
    List,
    Footer,
  },
  data() {
    return {
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 201, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true },
      // ],
      list: JSON.parse(localStorage.getItem("todolist")) || [],
      // 列表数据状态值：all , done , notdone
      state: "all",
    };
  },
  computed: {
    newList() {
      if (this.state === "done") {
        // 未完成的列表
        return this.list.filter((item) => item.isDone === true);
      } else if (this.state === "notdone") {
        // 已完成的列表
        return this.list.filter((item) => item.isDone === false);
      } else {
        // 全部列表
        return this.list;
      }
    },
  },
  methods: {
    // 改变列表状态
    changeState(state) {
      // console.log(state);
      this.state = state;
    },
    // 删除已完成
    delDone() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
    addTodo(name) {
      this.list.unshift({
        id: Date.now(),
        name,
        isDone: false,
      });
    },
    delTodo(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
  },
  watch: {
    // list(newValue) {
    //   console.log("列表变化：", newValue);
    // },
    list: {
      handler(newValue) {
        // console.log(newValue);
        localStorage.setItem("todolist", JSON.stringify(newValue));
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style>
</style>
